Background: I am trying to be able to make some unique identifier for each client that connects and stream to my service such that whenever they make a new request I can detect and block them from restreaming from my site until they stop the current stream.
DISCLAIMER: I am new to streaming, I was hoping that maybe on the New request object somewhere I would be able to tall if a user is streaming. I think somehow this is can be for socket application
Bellow is my code so far. The idea is to somehow be able to tell how many stream each client(Broswer) is streaming on each time they send request to my server.
app.get('/video', function (req, res) {
const path = 'abc.mp4'
const stat = fs.statSync(path)
const fileSize = stat.size
const range = req.headers.range
if (range) {
// console.log("Has range: ", range)
const parts = range.replace(/bytes=/, "").split("-")
const start = parseInt(parts[0], 10)
const end = parts[1]
? parseInt(parts[1], 10)
: fileSize - 1
const chunksize = (end - start) + 1
const file = fs.createReadStream(path, { start, end })
const head = {
'Content-Range': `bytes ${start}-${end}/${fileSize}`,
'Accept-Ranges': 'bytes',
'Content-Length': chunksize,
'Content-Type': 'video/mp4',
'MY_UNIQUE_ID': "Hello World" + new Date().toISOString()
}
res.writeHead(206, head)
file.pipe(res)
file.on("data", function (chunk) {
if (!!req.headers) {
//Expecting to also have MY_UNIQUE_ID
console.log(req.headers)
}
});
}
})